package com.dfk.bs.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.dfk.bs.common.utils.SelectThing;
import com.dfk.bs.entity.Eleapp;
import com.dfk.bs.entity.House;
import com.dfk.bs.dao.HouseMapper;
import com.dfk.bs.entity.Housetype;
import com.dfk.bs.entity.Room;
import com.dfk.bs.service.EleappService;
import com.dfk.bs.service.HouseService;
import com.dfk.bs.service.HousetypeService;
import com.dfk.bs.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 房屋表 服务实现类
 * </p>
 *
 * @author DDK
 * @since 2019-12-12
 */
@Service
public class HouseServiceImpl extends ServiceImpl<HouseMapper, House> implements HouseService {

    @Autowired
    private HousetypeService housetypeService;

    @Autowired
    private RoomService roomService;

    @Autowired
    private EleappService eleappService;
    @Override
    public void pageQuery(Page<House> pageParam, String selectname) {
        QueryWrapper<House> queryWrapper=new QueryWrapper<>();

        /*查询出来排序 根据表中的sort字段排序*/
        queryWrapper.orderByAsc("sort");
        if (selectname==null){
            baseMapper.selectPage(pageParam,queryWrapper);
            return;
        }


       /* int typeid = SelectId.getTypeId(selectname);*/


        /*两个条件  根据用房屋id或者房屋类型搜索分页*/
        if (!StringUtils.isEmpty(selectname)) {
            queryWrapper.like("housecode", selectname);
        }

        if (!StringUtils.isEmpty(selectname) ) {
            queryWrapper.or().like("typename", selectname);
        }

        baseMapper.selectPage(pageParam,queryWrapper);
    }

    //增加房屋同时增加房间
    @Override
    public void addRoomAndEleapp(String typename,String housecode) {
        //typename  房子的户型
        Housetype housetypeone = housetypeService.getOne(
                Wrappers.<Housetype>lambdaQuery()
                        .eq(Housetype::getName, typename), false
        );

        String roomcodes = housetypeone.getRoomcodes();

        String[] roomcodelist = roomcodes.split(",");

        for (String roomcode : roomcodelist) {
            Room room=new Room();
            room.setHousecode(housecode);
            room.setName(roomcode);
            room.setRoomcode(roomcode);
            //每个房间用电器
            String eleappcodes = SelectThing.getEleappcodes(roomcode);
            String[] eleappcodelist = eleappcodes.split(",");
            for (String eleappcode: eleappcodelist) {
                Eleapp eleapp=new Eleapp();
                eleapp.setHousecode(housecode);
                eleapp.setRoomcode(roomcode);
                eleapp.setName(eleappcode);
                eleapp.setEleappcode(eleappcode);
                eleappService.save(eleapp);
            }
            room.setEleappcodes(eleappcodes);
            roomService.save(room);
        }
    }
}
